﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XDYRestaurant
{
    public partial class CustomerInfo : Form
    {
        private Context.Context ctx = new Context.Context();
        //字段  单例模式
        private static CustomerInfo customerInfo;
        public CustomerInfo()
        {
            InitializeComponent();
        }
        //单例模式(对外提供实例化方法)
        public static CustomerInfo Create()
        {
            if (customerInfo != null)
            {
                customerInfo.Close();
            }
            customerInfo = new CustomerInfo();
            return customerInfo;
        }
        private void ShowAllInfo()
        {
            var list = ctx.Customers.Where(s => s.CustomerPhone.Contains(txtSearchPhone.Text)).ToList();
            txtSearchPhone.Text = "";
            dgvMemberInfoList.DataSource = list;
        }
        private void btbShowAllInfo_Click(object sender, EventArgs e)
        {
            ShowAllInfo();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string phone = txtId.Text.Trim().ToString();
            string password = txtPassword.Text.Trim().ToString();
            string name = txtName.Text.Trim().ToString();
            int id,f;
            Models.Customer customer = new Models.Customer();
            Models.Table table = new Models.Table();
            Int64 t;
            if (!Int64.TryParse(txtId.Text.Trim(), out t)||phone[0] == '-')
            {
                MessageBox.Show("请输入正确的顾客手机号！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (phone.Length != 11)
            {
                MessageBox.Show("请输入11位顾客手机号！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (name == "")
            {
                MessageBox.Show("请输入姓名！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (password.Length < 6)
            {
                MessageBox.Show("您的口令过于简单，请重新设置！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!int.TryParse(txtTableId.Text.Trim(), out f)&& txtTableId.Text.Trim().ToString() != "")
            {
                MessageBox.Show("请输入正确的餐桌号！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if(txtTableId.Text.Trim().ToString() != "")
            {
                 id = Convert.ToInt32(txtTableId.Text.Trim());
                 if (ctx.Tables.FirstOrDefault(s => s.TableId == id) == null)
                 {
                    MessageBox.Show("该餐桌号不存在！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                 }
                table = ctx.Tables.FirstOrDefault(s => s.TableId == id);
                if(table.TableStatic == 1)
                {
                    MessageBox.Show("该餐桌有其他顾客正在就餐！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    table.TableStatic = 1;
                }
                customer.TableId = id;
            }
            customer.CustomerPhone = phone;
            customer.CustomerName = name;
            customer.CustomerPassword = password;
            if (ctx.Customers.FirstOrDefault(s => s.CustomerPhone == phone) == null)
            {
                table.CustomerPhone = customer.CustomerPhone;
                ctx.Customers.Add(customer);
                ctx.SaveChanges();
                MessageBox.Show("添加成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtId.Text = "";
                ShowAllInfo();
            }
            else
            {
                try
                {
                    DialogResult res = MessageBox.Show("确认要修改吗?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (res != DialogResult.OK) return;
                    var customer2 = ctx.Customers.FirstOrDefault(s => s.CustomerPhone == phone);
                    customer2.CustomerPassword = password;
                    
                    if (txtTableId.Text.Trim().ToString() != "")
                    {
                        if(customer2.TableId != null)
                        {
                            MessageBox.Show("该顾客已有就餐餐桌！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        id = Convert.ToInt32(txtTableId.Text.Trim());
                        customer2.TableId = id;
                        table.TableStatic = 1;
                    }
                    else
                    {
                        if (customer2.TableId != null)
                        {
                            MessageBox.Show("该顾客正在就餐！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    table.CustomerPhone = customer2.CustomerPhone;
                    ctx.SaveChanges();
                    MessageBox.Show("修改成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtId.Text = "";
                    ShowAllInfo();
                }
                catch(Exception ex)
                {
                    MessageBox.Show("添加或修改失败!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtId.Text = "";
            txtPassword.Text = "";
            txtTableId.Text = "";
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dgvMemberInfoList.SelectedRows[0].Cells[0].Value.ToString();
                DialogResult res = MessageBox.Show("确认要删除吗?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (res == DialogResult.OK)
                {
                    var customer = ctx.Customers.FirstOrDefault(u => u.CustomerPhone == id);
                    ctx.Customers.Remove(customer);
                    ctx.SaveChanges();
                    MessageBox.Show("删除成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtId.Text = "";
                    ShowAllInfo();
                }
            }
            catch
            {
                MessageBox.Show("该顾客正在用餐！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void dgvMemberInfoList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                txtId.Text = dgvMemberInfoList.SelectedRows[0].Cells[0].Value.ToString();
                txtPassword.Text = dgvMemberInfoList.SelectedRows[0].Cells[1].Value.ToString();
                if(dgvMemberInfoList.SelectedRows[0].Cells[3].Value == null)
                {
                    txtTableId.Text = "";
                }
                else
                {
                    txtTableId.Text = dgvMemberInfoList.SelectedRows[0].Cells[3].Value.ToString();
                }
            }
            catch
            {
                //兼容性：如果选中非法就什么也不干
                return;
            }
        }
    }
}
